package com.boe.hbd.server.util;

public class Distance {
	private static final double EARTH_RADIUS = 6378137.0;

	/**
	 * 
	 * @param lat_a
	 * @param lng_a
	 * @param lat_b
	 * @param lng_b
	 * @return meter
	 */
	public static double distance(double lat_a, double lng_a, double lat_b,
			double lng_b) {
		double radLat1 = (lat_a * Math.PI / 180.0);
		double radLat2 = (lat_b * Math.PI / 180.0);
		double a = radLat1 - radLat2;
		double b = (lng_a - lng_b) * Math.PI / 180.0;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	public static void main(String[] args) {
		long b = System.currentTimeMillis();
		for (int i = 0; i < 1000000; i++) {
//			System.out.println(distance(31.443362, 118.742875, 30.420419,
//					117.455064));
			distance(31.443362, 118.742875, 30.420419,
					117.455064);
		}
		long e = System.currentTimeMillis();
		System.out.println(e-b);
	}
}
